package com.cal.utms.dao.payment;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.cal.utms.entity.CustomerInvestment;
import com.cal.utms.entity.CustomerRedemptionRequest;
import com.cal.utms.entity.CustomerTransactionLog;
import com.cal.utms.entity.MoneyTransaction;
import com.cal.utms.entity.RedemptionReqToRedemtion;
import com.cal.utms.util.HibernateUtil;

public class RedemptionDaoImpl implements RedemptionDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<CustomerRedemptionRequest> getRedemptionRequest() {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<CustomerRedemptionRequest> redemptionRequests = new ArrayList<CustomerRedemptionRequest>();
		try {
			tx = session.beginTransaction();
			redemptionRequests = session.createQuery("from CustomerRedemptionRequest").list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return redemptionRequests;
	}

	@SuppressWarnings("unchecked")
	public List<RedemptionReqToRedemtion> getRedemtion(int redemtion_id) {

		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<RedemptionReqToRedemtion> redemtions = new ArrayList<RedemptionReqToRedemtion>();
		try {
			tx = session.beginTransaction();
			redemtions = session.createQuery("from RedemptionReqToRedemtion R where R.customerRedemptionRequest.id=:id").setParameter("id", redemtion_id).list();
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null)
				tx.rollback();

		} finally {
			session.close();
		}
		return redemtions;
	}

	@Override
	public Object saveRedemtioRequest(CustomerRedemptionRequest request) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		Object object = null;
		try {
			transaction = session.beginTransaction();
			object = session.save(request);
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return object;
	}

	public BigDecimal getTotalRedemUnits(int id) {
		BigDecimal count = BigDecimal.ZERO;
		Session session = HibernateUtil.getSession().openSession();
		Transaction tr = null;
		try {
			tr = session.beginTransaction();
			count = (BigDecimal) session.createQuery("Select SUM(R.releaseUnits) from RedemptionReqToRedemtion R where R.customerInvestment.id=:id").setParameter("id", id)
					.uniqueResult();
			tr.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}

		return count;
	}

	@Override
	public CustomerRedemptionRequest getById(int id) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		CustomerRedemptionRequest redemptionRequest = null;
		try {
			transaction = session.beginTransaction();
			redemptionRequest = (CustomerRedemptionRequest) session.get(CustomerRedemptionRequest.class, id);
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return redemptionRequest;
	}

	public Object confirm(CustomerRedemptionRequest customerRedemptionRequest, List<MoneyTransaction> moneyTransactions, List<CustomerInvestment> customerInvestments,
			List<CustomerTransactionLog> customerTransactionLogs, CustomerRedemptionRequest monthlyRedempRequest, CustomerInvestment precustomerInvestment) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tr = null;
		Object object = null;
		try {
			tr = session.beginTransaction();
			object = session.merge(customerRedemptionRequest);
			for (MoneyTransaction moneyTransaction : moneyTransactions) {
				session.save(moneyTransaction);
			}
			for (CustomerInvestment customerInvestment : customerInvestments) {
				session.merge(customerInvestment);
			}
			for (CustomerTransactionLog customerTransactionLog : customerTransactionLogs) {
				session.save(customerTransactionLog);
			}
			if (monthlyRedempRequest != null) {
				session.save(monthlyRedempRequest);
			}
			if (precustomerInvestment != null) {
				session.merge(precustomerInvestment);
			}
			tr.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return object;
	}

	public Object updateRedemtioRequest(CustomerRedemptionRequest request) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		Object object = null;
		try {
			transaction = session.beginTransaction();
			session.createQuery("delete from RedemptionReqToRedemtion R where R.customerRedemptionRequest.id=:reqid").setParameter("reqid", request.getId()).executeUpdate();
			object = session.merge(request);
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return object;
	}

	@SuppressWarnings("unchecked")
	public List<CustomerRedemptionRequest> getRequestByinvestment(int id) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<CustomerRedemptionRequest> redemptionRequests = new ArrayList<CustomerRedemptionRequest>();
		try {
			tx = session.beginTransaction();
			redemptionRequests = session.createQuery("select customerRedemptionRequest from RedemptionReqToRedemtion R where R.customerInvestment.id=:invid")
					.setParameter("invid", id).list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return redemptionRequests;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CustomerRedemptionRequest> getConfirmedRedemptionRequest() {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<CustomerRedemptionRequest> redemptionRequests = new ArrayList<CustomerRedemptionRequest>();
		try {
			tx = session.beginTransaction();
			redemptionRequests = session.createQuery("from CustomerRedemptionRequest C where C.status=:stat").setParameter("stat", "CONFIRM").list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return redemptionRequests;
	}

	public void cancelRedemption(CustomerRedemptionRequest customerRedemptionRequest, List<MoneyTransaction> transactions, List<CustomerTransactionLog> customerTransactionLogs) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.merge(customerRedemptionRequest);
			for (MoneyTransaction moneyTransaction : transactions) {
				session.save(moneyTransaction);
			}
			for (CustomerTransactionLog customerTransactionLog : customerTransactionLogs) {
				session.save(customerTransactionLog);
			}
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<CustomerRedemptionRequest> getRedemption(int customer_id, int invest_fund_id) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<CustomerRedemptionRequest> redemptionRequests = new ArrayList<CustomerRedemptionRequest>();
		try {
			tx = session.beginTransaction();
			redemptionRequests = session.createQuery("from CustomerRedemptionRequest R where R.customer.id=:cusid and R.fund.id=:fundid and R.status=:sts")
					.setParameter("cusid", customer_id).setParameter("fundid", invest_fund_id).setParameter("sts", "INITIAL").list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			// session.close();
		}
		return redemptionRequests;
	}
}
